import sys
import os
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.dist import use_library
use_library('django', '0.96')
from os import environ
import models
import datetime
import string	


class MainPage(webapp.RequestHandler,db.Model):
	def verifyLogin(self):
		user = users.get_current_user()
		if user:
			usercheck=models.userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='user':
						return
					elif i.approved==False:
						self.redirect("/usererror")
						return
				self.redirect("/admin")
			except:
				path = os.path.join(os.path.dirname(__file__),'form.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.verifyLogin()
		ids=''
		url = self.request.url
		for i in range(0,len(url),1):
			if(url[i]=='?'):
				i=i+1
				while(url[i]!='='):
					ids=ids+str(url[i])
					i=i+1
				break
		ids=str(ids)
		self.response.out.write(str(ids))
		path = os.path.join(os.path.dirname(__file__),'choice.html')#use template
		self.response.out.write(template.render(path,{"ids":ids}))#using template

application = webapp.WSGIApplication([('/choice', MainPage)],debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

